package com.yanjr.modules.ad.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yanjr.common.persistence.Page;
import com.yanjr.common.utils.IdGen;
import com.yanjr.common.utils.StringUtils;
import com.yanjr.modules.ad.dao.AdSpaceDao;
import com.yanjr.modules.ad.entity.AdSpace;
import com.yanjr.modules.ad.service.AdSpaceService;

@Service("adSpaceService")
@Transactional(readOnly = true)
public class AdSpaceServiceImpl implements AdSpaceService {
	
	@Autowired
	private AdSpaceDao adSpaceDao;

	@Override
	public AdSpace get(String id) {
		return adSpaceDao.get(id);
	}

	@Override
	public Page<AdSpace> find(Page<AdSpace> page, AdSpace adSpace) {
		StringBuffer sql = new StringBuffer("select * from ad_space where 1=1 ");
		if(StringUtils.isNotEmpty(adSpace.getName())) {
			sql.append(" name like '%" + adSpace.getName() + "%' ");
		}
		return adSpaceDao.findPage(page, sql.toString());
	}

	@Override
	public List<AdSpace> allData() {
		return adSpaceDao.findAll();
	}

	@Override
	@Transactional(readOnly = false)
	public void save(AdSpace adSpace) {
		if(StringUtils.isEmpty(adSpace.getId())) {
			adSpace.setId(IdGen.uuid());
			adSpaceDao.save(adSpace);
		} else {
			adSpaceDao.update(adSpace);
		}
		
	}

	@Override
	@Transactional(readOnly = false)
	public void delete(String id) {
		adSpaceDao.delete(id);
	}

}
